package com.chengh.nami.core.security.handler;

import com.chengh.nami.common.response.R;
import com.chengh.nami.common.response.ResponseCodeEnum;
import com.chengh.nami.common.utils.ResultUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 自定义无权限处理类
 *
 * @author chengh
 */
@Slf4j
@Component
public class UserAuthAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) {
        log.error("用户权限不足");
        R<Void> failed = R.failed(ResponseCodeEnum.FAILED_403.getCode(), ResponseCodeEnum.FAILED_403.getMessage());
        ResultUtils.responseJson(response, failed);
    }
}
